#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<set>
using namespace std;

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        set<int> s1(nums1.begin(), nums1.end());
        set<int> s2(nums2.begin(), nums2.end());
        auto it1 = s1.begin();
        auto it2 = s2.begin();
        vector<int> ans;
        while (it1 != s1.end() && it2 != s2.end())
        {
            if (*it1 > *it2) ++it2;
            else if (*it1 < *it2) ++it1;
            else
            {
                ans.push_back(*it1);
                ++it1, ++it2;
            }
        }
        return ans;
    }

    struct ListNode {
        int val;
        ListNode* next;
        ListNode(int x) : val(x), next(NULL) {}
        
    };

    ListNode* detectCycle(ListNode* head) {
        set<ListNode*> s;
        ListNode* cur = head;
        while (cur)
        {
            if (s.count(cur)) return cur;
            else s.insert(cur);
            cur = cur->next;
        }
        return nullptr;
    }
};